﻿using Protocol.Dto;
using UnityEngine.UI;

namespace Script.UI._1.Main
{
    public class InfoPanel : UIBase
    {
        private void Awake()
        {
            Bind(UIEvent.REFRESH_INFO_PANEL);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.REFRESH_INFO_PANEL:
                    UserDto dto = message as UserDto;
                    if (dto != null) RefreshView(dto.Name, dto.Lv, dto.Exp, dto.Bean);
                    break;
            }
        }

        // private Image imgHead;   // 用于扩展换头像功能
        private Text txtName;
        private Text txtLv;
        private Slider Slider;
        private Text txtExp;
        private Text txtBean;

        // Start is called before the first frame update
        void Start()
        {
            txtName = transform.Find("NameT").GetComponent<Text>();
            txtLv = transform.Find("LVT").GetComponent<Text>();
            Slider = transform.Find("Slider").GetComponent<Slider>();
            txtExp = transform.Find("ExpT").GetComponent<Text>();
            txtBean = transform.Find("BeanT").GetComponent<Text>();
        }

        // Update is called once per frame

        /// <summary>
        /// 刷新视图
        /// </summary>
        private void RefreshView(string userName, int lv, int exp, int bean)
        {
            txtName.text = userName;
            txtLv.text = "Lv." + lv;
            txtExp.text = exp + "/" + lv * 100;
            txtBean.text = "x " + bean;
            Slider.value = (float) exp / (lv * 100);
        }
    }
}